<?php

namespace addons\video\user\controller;

class Comments extends \web\user\controller\AddonUserBase{
    
    public function index(){
        return $this->fetch();
    }
    
    public function loadList(){
        $keyword = $this->_get('keyword');
        $startDate = $this->_get('startDate');
        $endDate = $this->_get("endDate");
        $filter = ' type=1 and is_delete=0 ';
        if ($startDate != null && $endDate != null)
            $filter .= ' and (a.create_time >= \'' . $startDate . ' 00:00:00\' and a.create_time <= \'' . $endDate . ' 23:59:59\')';
        elseif ($startDate != null)
            $filter .= ' and (a.create_time >= \'' . $startDate . ' 00:00:00\')';
        elseif ($endDate != null)
            $filter .= ' and (a.create_time <= \'' . $endDate . ' 23:59:59\')';

        if ($keyword != null) {
            $filter .= ' and b.username like \'%' . $keyword . '%\' ';
        }

        $m = new \addons\comments\model\Comments();
        $fields = 'a.id,a.video_id,a.type,a.content,a.create_time,b.username,c.video_title';
        $total = $m->getCommentsTotal($filter);
        $rows = $m->getCommentsList($this->getPageIndex(), $this->getPageSize(), $fields, $filter, 'a.id desc');
        return $this->toDataGrid($total, $rows);
    }

    /**
     * 逻辑删除
     * @return array
     */
    public function del(){
        $id = $this->_post('id');
        $m = new \addons\comments\model\Comments();
        try{
            $com = $m->where(['id' => $id])->find();
            if(!$com) return $this->failData('评论不存在');

            $ret = $m->where(['id' => $id])->update(['is_delete' => 1, 'update_time' => NOW_DATETIME]);
            if($ret < 1) return $this->failData('删除失败');

            //添加管理日志
            $this->addLog(self::LOG_DEL,'删除社区',$com, $id);
            return $this->successData();
        } catch (\Exception $ex) {
            return $this->failData($ex->getMessage());
        }
    }

}


